package edu.msg.hrappy.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.msg.hrappy.backend.model.Candidate;
import edu.msg.hrappy.backend.model.Comment;
import edu.msg.hrappy.backend.model.Job;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.CandidateService;
import edu.msg.hrappy.backend.service.CommentService;
import edu.msg.hrappy.backend.service.JobService;
import edu.msg.hrappy.backend.service.UserService;

public class DeleteCommentToCandidateServlet  extends HttpServlet {


		private static final long serialVersionUID = 1L;
		@EJB
	    private CandidateService candidateService;
	    @EJB
	    private UserService userService;
	    @EJB 
	    private CommentService commentService;
	    @EJB 
	    private JobService jobService;

	    @Override
	    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
	        req.getSession().setAttribute("er", null);
	        HttpSession session = req.getSession();
	        session.setAttribute("jobId", req.getParameter("jobId"));
	        User user = userService.getUserByUserName(req.getUserPrincipal()
	                .toString());
	        session.setAttribute("userId", user.getId());
	        session.setAttribute("username", user.getUserName());

	        Job job = jobService.getById(Long.parseLong(req.getParameter("jobId")));
	 
			
			  List<Candidate> candidates = job.getCandidates();
		        session.setAttribute("candidates", candidates);
	        String address = "./showCandidates.jsp";

	        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

	        dispatcher.forward(req, resp);

	    }

	    @Override
	    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
	        
	    	Comment comment = null;
	    	Candidate candidate = null;
	    	String candidateId = (String) req.getParameter("candidateId");
			candidate = candidateService.getById(Long.parseLong(candidateId));

			String commentId = (String) req.getParameter("comId");
			comment = commentService.getById(Long.parseLong(commentId));
	      
	     
			candidate.removeComment(comment);
	        candidateService.update(candidate);
	        
	        commentService.delete(comment);
	        
	        HttpSession session = req.getSession();
	    	
	        User user = userService.getUserByUserName(req.getUserPrincipal()
					.toString());
			session.setAttribute("userId", user.getId());
			session.setAttribute("username", user.getUserName());
			
			   Job job = jobService.getById(Long.parseLong(req.getParameter("jobId")));
				 
				
				  List<Candidate> candidates = job.getCandidates();
			        session.setAttribute("candidates", candidates);
		        String address = "./showCandidates.jsp";

	        RequestDispatcher dispatcher = req.getRequestDispatcher(address);

	        dispatcher.forward(req, resp);

	    }
}
